package com.hippo.core.vm

import android.R.attr.checked
import android.util.Log
import androidx.compose.runtime.getValue
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.setValue
import androidx.lifecycle.ViewModel
import com.hippo.core.core.ext.toJson
import com.hippo.core.core.manager.AppStore
import com.hippo.core.state.NotificationState
import com.hippo.core.utils.UiHandler
import com.thingclips.smart.home.sdk.ThingHomeSdk
import com.thingclips.smart.home.sdk.callback.IThingResultCallback
import com.thingclips.smart.sdk.api.IThingDataCallback
import com.thingclips.smart.sdk.bean.message.MessageListBean
import com.thingclips.smart.sdk.bean.message.MessageType
import com.thingclips.smart.sdk.bean.push.PushStatusBean
import com.thingclips.smart.sdk.bean.push.PushType


class NotificationVM : ViewModel() {

    var uiState by mutableStateOf(NotificationState())

    companion object{
        private const val TAG = "NotificationVM"
    }

    init {
//        getNotificationList(MessageType.MSG_REPORT)
    }

    fun updateState(state: NotificationState) {
        uiState = state
    }




    fun getNotificationList( msgType: MessageType) {

        updateState(uiState.copy(isLoading = true,messageType = msgType))

        ThingHomeSdk.getMessageInstance()
            .getMessageListByMsgType(0,100,msgType,object : IThingDataCallback<MessageListBean?>{

                override fun onSuccess(result: MessageListBean?) {
                    Log.d(TAG, "onSuccess: ${result.toJson()}")
                    updateState(uiState.copy(notificationList = result?.datas.orEmpty()))
                }

                override fun onError(errorCode: String, errorMessage: String) {
                    Log.e(TAG, "onError: $errorMessage" )
                    UiHandler.showToast(errorMessage)
                }
            })

    }

    fun getPushStatus(callback:(Boolean)->Unit){
//        ThingHomeSdk.getPushInstance()
//            .getPushStatus(object : IThingResultCallback<PushStatusBean?> {
//                override fun onSuccess(result: PushStatusBean?) {
//                    Log.d(TAG, "onSuccess: ${result.toJson()}")
//                    callback(result?.isPushEnable == "1")
//                }
//                override fun onError(errorCode: String, errorMessage: String) {
//                    Log.e(TAG, "onError: $errorMessage")
//                    callback(false)
//                }
//            })

        val isOpen = AppStore.readBoolean("push_status",true)
        callback(isOpen)
    }

    fun setPushStatus(isOpen:Boolean,callback:(Boolean)->Unit){
//        ThingHomeSdk.getPushInstance()
//            .setPushStatus(isOpen,object : IThingDataCallback<Boolean> {
//                override fun onSuccess(result: Boolean) {
//                    Log.d(TAG, "onSuccess: $result")
//                    callback(result)
//                }
//
//                override fun onError(errorCode: String, errorMessage: String) {
//                    Log.e(TAG, "onError: $errorMessage")
//                    UiHandler.showToast(errorMessage)
//                }
//            })


        AppStore.save("push_status",isOpen)
        callback(true)
    }


}